package edu.rakovic.elearning.beans;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import edu.rakovic.elearning.interfaces.Application;
import edu.rakovic.elearning.interfaces.Content;
import edu.rakovic.elearning.interfaces.Course;
import edu.rakovic.elearning.interfaces.Exam;
import edu.rakovic.elearning.interfaces.User;
import edu.rakovic.elearning.service.Applications;
import edu.rakovic.elearning.service.Courses;

public class CourseBean implements Course {
	
	private String name;
	private String shortDescription;
	private User teacher;
	private List<Application> userApplication = new LinkedList<Application>();
	private List<Content> courseContent = new LinkedList<Content>();
	
	//connection with exam
	private List<Exam> examList = new LinkedList<Exam>();
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getShortDescription() {
		return shortDescription;
	}
	public void setShortDescription(String shortDescription) {
		this.shortDescription = shortDescription;
	}
	
	public Course createCourse(String name, String shortDescription, User teacher){
		teacher.getTeachingCourses().add(this);
		this.setName(name);
		this.setShortDescription(shortDescription);
		this.setTeacher(teacher);
		
		return this;
	}
	
	public Course removeCourse(Courses courses, Applications applications){
		//removing connections with Teacher
		getTeacher().getTeachingCourses().remove(this);
		cancelAllCourseApplications(applications);
		courses.delete(this.getName());
		return this;
	}
	
	private void cancelAllCourseApplications(Applications applications) {
		Collection<Application> courseList = applications.retrieveCourseApplications(this.getName());		
		for (Application appl : courseList) {
			//removing single application from "database"
			appl.cancelApplication(applications);
		}
	}
	
	public double averageGrade(){
		//sum of all grades
		double sum =0;
		//how many time exam has been taken
		double size = 0;
		for (Exam singleExam : examList) {
			size = examList.size();
			sum+= singleExam.getGrade();
		}
		//average
		return sum/size;
		
	}
	
	public void setUserApplication(List<Application> userApplication) {
		this.userApplication = userApplication;
	}
	public List<Application> getUserApplication() {
		return userApplication;
	}
	public void setTeacher(User teacher) {
		this.teacher = teacher;
	}
	public User getTeacher() {
		return teacher;
	}
	public void setExamList(List<Exam> examList) {
		this.examList = examList;
	}
	public List<Exam> getExamList() {
		return examList;
	}
	public void setCourseContent(List<Content> courseContent) {
		this.courseContent = courseContent;
	}
	public List<Content> getCourseContent() {
		return courseContent;
	}

}
